import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pp_uikit/pp_button.dart';
import 'package:pp_uikit/pp_dialog.dart';
import 'package:pp_uikit/pp_uikit_constants.dart';
import 'package:pp_uikit/theme/pp_default_theme.dart';

class PPSingleSelectSheetTheme {
  /// 标题高度
  static double titleHeight = 52.0;

  /// 选项高度
  static double itemHeight = 42.0;

  /// 按钮高度
  static double bottomHeight = 62.0;
}

// ignore: must_be_immutable
class PPSingleSelectSheet extends StatefulWidget {
  /// 标题
  final String title;

  /// 标题字体大小
  final double? titleFontSize;

  /// 弹窗样式
  final PPDialogStyle dialogStyle;

  /// 最多个数
  final int maxItemCounts;

  /// 最少个数
  final int minItemCounts;

  /// 标题字体粗度
  final FontWeight? titleFontWeight;

  //选择列表
  final List<String> items;

  // 选择索引
  int? selectedIndex;

  final String buttonTitle;

  final TextAlign textAlign;

  final Function(int)? confirmFun; //确认回调

  PPSingleSelectSheet({
    Key? key,
    required this.title,
    required this.items,
    this.dialogStyle = PPDialogStyle.orange,
    this.titleFontSize,
    this.titleFontWeight,
    this.maxItemCounts = 9,
    this.minItemCounts = 2,
    this.selectedIndex,
    this.buttonTitle = "确认",
    this.textAlign = TextAlign.center,
    this.confirmFun,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _PPSingleSelectSheet();
  }
}

class _PPSingleSelectSheet extends State<PPSingleSelectSheet> {
  int _savedChoosePos = -1;

  @override
  Widget build(BuildContext context) {
    if (widget.selectedIndex != null) {
      _savedChoosePos = widget.selectedIndex!;
    }
    return Material(
      type: MaterialType.transparency,
      child: Container(
        height: _sheetHeight(),
        decoration: const ShapeDecoration(
          color: Colors.white,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(12), topRight: Radius.circular(12)),
          ),
        ),
        child: Column(
          children: <Widget>[
            //头部布局
            Container(
                padding: const EdgeInsets.only(left: 9, right: 9),
                height: PPSingleSelectSheetTheme.titleHeight,
                child: Stack(children: <Widget>[
                  Align(
                    alignment: Alignment.center,
                    child: Text(
                      widget.title,
                      style: TextStyle(
                        color: PPDefaultTheme.instance.colorTextBase,
                        fontWeight: widget.titleFontWeight,
                        fontSize: widget.titleFontSize,
                      ),
                    ),
                  ),
                  GestureDetector(
                    onTap: () {
                      Navigator.pop(context);
                    },
                    child: Align(
                      alignment: Alignment.centerRight,
                      child: Image.asset(
                        "images/icon_close_30.png",
                        fit: BoxFit.cover,
                        width: 25,
                        height: 25,
                        package: PPUIKitConstant.packageName,
                      ),
                    ),
                  ),
                ])),

            //列表布局
            Expanded(
              flex: 1,
              child: ListView.builder(
                shrinkWrap: true,
                itemCount: widget.items.length,
                itemBuilder: (context, index) {
                  return _itemView(index);
                },
              ),
            ),
            Container(
              height: PPSingleSelectSheetTheme.bottomHeight - 10,
              width: double.infinity,
              padding: const EdgeInsets.only(left: 12, right: 12, top: 10),
              decoration: const BoxDecoration(
                  border: Border(
                top: BorderSide(color: PPDefaultTheme.lineColor, width: 1),
              )),
              child: PPButton(
                widget.buttonTitle,
                height: 42,
                onPressed: () {
                  //确认按钮点击事件
                  if (_savedChoosePos != -1 && widget.confirmFun != null) {
                    Navigator.pop(context);
                    widget.confirmFun!(_savedChoosePos);
                  }
                },
                fontSize: 16,
                textColor: Colors.white,
                paddingHorizontal: 10,
                radius: 21,
                bgGradientColor: _bgGradientColor(),
              ),
            ),
            ScreenUtil().bottomBarHeight > 0
                ? SizedBox(height: ScreenUtil().bottomBarHeight)
                : const SizedBox(height: 10)
          ],
        ),
      ),
    );
  }

  List<Color> _bgGradientColor() {
    if (widget.dialogStyle == PPDialogStyle.blue) {
      return [
        const Color(0xFF667EF8),
        const Color(0xFF4057EC),
      ];
    }
    return [
      PPDefaultTheme.instance.gradientLeft,
      PPDefaultTheme.instance.gradientRight,
    ];
  }

  //单选列表布局
  Widget _itemView(int index) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      child: Container(
        padding: const EdgeInsets.only(left: 16, right: 16),
        height: PPSingleSelectSheetTheme.itemHeight,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: Text(
                widget.items[index],
                style: TextStyle(
                  fontSize: 16,
                  color: PPDefaultTheme.instance.colorTextBase,
                ),
                textAlign: widget.textAlign,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            Image.asset(
              (_savedChoosePos == index)
                  ? widget.dialogStyle == PPDialogStyle.orange
                      ? "images/btn_checkmark_on.png"
                      : "images/icon_selector_select_blue.png"
                  : "images/icon_selector_normal.png",
              width: 18,
              height: 18,
              package: PPUIKitConstant.packageName,
              color: widget.dialogStyle == PPDialogStyle.normal
                  ? PPDefaultTheme.instance.brandPrimary
                  : null,
            ),
          ],
        ),
      ),
      onTap: () {
        setState(() {
          _savedChoosePos = index;
          widget.selectedIndex = index;
        });
      },
    );
  }

  // 计算单选框高度
  double _sheetHeight() {
    double titleHeight = PPSingleSelectSheetTheme.titleHeight;
    double itemHeight = PPSingleSelectSheetTheme.itemHeight;
    double bottomHeight = PPSingleSelectSheetTheme.bottomHeight;
    double bottomBarHeight = 10;
    if (ScreenUtil().bottomBarHeight > 0) {
      bottomBarHeight = PPSingleSelectSheetTheme.bottomHeight;
    }
    if (widget.items.length < widget.minItemCounts) {
      // 最小个数
      return titleHeight +
          itemHeight * widget.minItemCounts +
          bottomHeight +
          bottomBarHeight;
    } else if (widget.items.length < widget.maxItemCounts) {
      // min > count < max
      return titleHeight +
          itemHeight * widget.items.length +
          bottomHeight +
          bottomBarHeight;
    } else {
      // 最多个数
      return titleHeight +
          itemHeight * widget.maxItemCounts +
          bottomHeight +
          bottomBarHeight;
    }
  }
}
